<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <el-input v-model="query.fundCode" clearable size="small" placeholder="输入基金代码搜索" style="width: 200px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <el-select
          v-model="query.fundType"
          clearable
          size="small"
          class="filter-item"
          placeholder="请选择基金类型"
          @change="crud.toQuery"
        >
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
        <el-select
          v-model="query.purchaseChannel"
          clearable
          size="small"
          class="filter-item"
          placeholder="购买渠道"
          @change="crud.toQuery"
        >
          <el-option
            v-for="item in channelOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
        <rrOperation />
        <el-select
          v-model="query.sortBy"
          clearable
          size="mini"
          class="filter-item"
          placeholder="请选择排序"
          @change="crud.toQuery"
        >
          <el-option
            v-for="item in sortOptions"
            :key="item.value"
            width="50"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
        <el-select
          v-model="query.sortDirection"
          clearable
          size="mini"
          class="filter-item"
          placeholder="请选择排序"
          @change="crud.toQuery"
        >
          <el-option
            v-for="item in sortDirection"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>

      </div>
      <crudOperation :permission="permission">
        <!-- 任务日志 -->
        <el-button
          slot="right"
          class="filter-item"
          size="mini"
          type="info"
          icon="el-icon-tickets"
          @click="doLog"
        >交易日志</el-button>
      </crudOperation>
      <Log ref="log" />
    </div>
    <!--表单渲染-->
    <el-dialog
      append-to-body
      :close-on-click-modal="false"
      :before-close="crud.cancelCU"
      :visible.sync="crud.status.cu > 0"
      :title="crud.status.title"
      width="500px"
    >
      <el-form
        ref="form"
        :inline="true"
        :model="form"
        :rules="rules"
        size="small"
        label-width="80px"
      >
        <el-form-item
          label="基金编码"
          prop="fundCode"
        >
          <el-input
            v-model="form.fundCode"
            style="width:370px;"
          />
        </el-form-item>
        <el-form-item
          label="基金名称"
          prop="fundName"
        >
          <el-input
            v-model="form.fundName"
            style="width:370px;"
            :disabled="true"
          />
        </el-form-item>
        <el-form-item
          label="基金类型"
          prop="fundType"
        >
          <el-select v-model="form.fundType" placeholder="请选择">
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item
          label="购买渠道"
          prop="purchaseChannel"
        >
          <el-select v-model="form.purchaseChannel" placeholder="请选择">
            <el-option
              v-for="item in channelOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item
          label="购买成本"
          prop="principalNetValue"
        >
          <el-input
            v-model="form.principalNetValue"
            style="width:370px;"
          />
        </el-form-item>
        <el-form-item
          label="持有份额"
          prop="count"
        >
          <el-input
            v-model="form.count"
            style="width:370px;"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="text" @click="crud.cancelCU">取消</el-button>
        <el-button :loading="crud.status.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
      </div>
    </el-dialog>
    <!--表格渲染-->
    <el-table
      ref="table"
      v-loading="crud.loading"
      :data="crud.data"
      row-key="id"
      :row-class-name="tableRowClassName"
      @selection-change="crud.selectionChangeHandler"
    >
      <el-table-column type="selection" width="55" />
      <el-table-column :show-overflow-tooltip="true" label="基金代码" width="125px" prop="fundCode" />

      <el-table-column :show-overflow-tooltip="true" width="180" prop="fundName" label="基金名称">
        <template slot-scope="scope">
          {{ scope.row.fundName }}
          <br>
          <span v-if="scope.row.fundName === '合计'" />
          <el-tag v-if="scope.row.purchaseChannel === 1" type="success">蚂蚁财富</el-tag>
          <el-tag v-else-if="scope.row.purchaseChannel === 2" type="success">天天基金</el-tag>
          <el-tag v-else-if="scope.row.purchaseChannel === 3" type="success">京东金融</el-tag>
          <el-tag v-else-if="scope.row.purchaseChannel === 4" type="danger">招商银行</el-tag>
          <span v-if="scope.row.fundName === '合计'" />
          <el-tag v-else-if="scope.row.fundType === 1" type="danger">股票基金</el-tag>
          <el-tag v-else-if="scope.row.fundType === 2" type="danger">债券基金</el-tag>
          <el-tag v-else type="danger">other</el-tag>
        </template>
      </el-table-column>
      <!-- <el-table-column :show-overflow-tooltip="true" prop="fundType" label="类型">
        <template slot-scope="scope">
          <span v-if="scope.row.fundName === '合计'" />
          <span v-else-if="scope.row.fundType === 1">股票基金</span>
          <span v-else-if="scope.row.fundType === 2">债券基金</span>
          <span v-else>other</span>
        </template>
      </el-table-column> -->
      <el-table-column :show-overflow-tooltip="true" prop="count" label="持仓份额" />

      <el-table-column :show-overflow-tooltip="true" prop="principalAmount" label="持仓成本">
        <template slot-scope="scope">
          <span>{{ scope.row.principalAmount | rounding | thousands }}</span>
          <br>
          <span>{{ scope.row.principalNetValue }}</span>
        </template>
      </el-table-column>

      <el-table-column :show-overflow-tooltip="true" width="120" prop="newestNetValue" label="持仓金额">
        <template slot-scope="scope">
          <span>{{ scope.row.newestAmount | rounding | thousands }}</span>
          <br>
          <span v-if="scope.row.fundName === '合计'" />
          <span v-else>{{ scope.row.newestNetValue }}({{ scope.row.newestUpdatedDate | formatDate }})</span>
        </template>
      </el-table-column>

      <el-table-column :show-overflow-tooltip="true" prop="profitAmount" label="持仓盈亏">
        <template slot-scope="scope">
          <span v-if="scope.row.profitAmount > 0" style="color:red;">{{ scope.row.profitAmount | rounding | thousands }}</span>
          <span v-else-if="scope.row.profitAmount < 0" style="color:green;">{{ scope.row.profitAmount | rounding | thousands }}</span>
          <span v-else>{{ scope.row.profitAmount | rounding }}</span>
          <br>
          <span v-if="scope.row.profitRate > 0" style="color:red;">{{ scope.row.profitRate * 100 | rounding }}%</span>
          <span v-else-if="scope.row.profitRate < 0" style="color:green;">{{ scope.row.profitRate * 100 | rounding }}%</span>
          <span v-else>{{ scope.row.profitRate * 100 | rounding }}%</span>
        </template>
      </el-table-column>

      <el-table-column :show-overflow-tooltip="true" prop="profitAmountOfYesterday" label="昨日盈亏">
        <template slot-scope="scope">
          <span v-if="!scope.row.profitAmountOfYesterday" />
          <span v-else-if="scope.row.profitAmountOfYesterday > 0" style="color:red;">{{ scope.row.profitAmountOfYesterday | rounding | thousands }}</span>
          <span v-else-if="scope.row.profitAmountOfYesterday < 0" style="color:green;">{{ scope.row.profitAmountOfYesterday | rounding | thousands }}</span>
          <span v-else>{{ scope.row.profitAmountOfYesterday | rounding }}</span>
          <br>
          <span v-if="!scope.row.profitRateOfYesterday" />
          <span v-else-if="scope.row.profitRateOfYesterday > 0" style="color:red;">{{ scope.row.profitRateOfYesterday * 100 | rounding }}%</span>
          <span v-else-if="scope.row.profitRateOfYesterday < 0" style="color:green;">{{ scope.row.profitRateOfYesterday * 100 | rounding }}%</span>
          <span v-else>{{ scope.row.profitRateOfYesterday * 100 | rounding }}%</span>
        </template>
      </el-table-column>

      <el-table-column :show-overflow-tooltip="true" prop="dealAmountOfCurrentMonth" label="当月买入">
        <template slot-scope="scope">
          <span v-if="scope.row.dealAmountOfCurrentMonth > 0" style="color:red;">{{ scope.row.dealAmountOfCurrentMonth | rounding | thousands }}</span>
          <span v-else-if="scope.row.dealAmountOfCurrentMonth < 0" style="color:green;">{{ scope.row.dealAmountOfCurrentMonth | rounding | thousands }}</span>
          <span v-else>{{ scope.row.dealAmountOfCurrentMonth | rounding | thousands }}</span>
          <br>
          <span v-if="scope.row.dealAmountOfCurrentMonthOnOrder > 0">{{ scope.row.dealAmountOfCurrentMonthOnOrder | rounding | thousands }}</span>
          <el-tag v-if="scope.row.dealAmountOfCurrentMonthOnOrder > 0" type="info">在途</el-tag>
        </template>
      </el-table-column>

      <el-table-column
        v-if="checkPer(['admin','userfund:edit','userfund:del'])"
        label="操作"
        width="130px"
        align="center"
        fixed="right"
      >
        <template slot-scope="scope">
          <udOperation
            v-if="scope.row.fundName !== '合计'"
            :data="scope.row"
            :permission="permission"
          />
        </template>
      </el-table-column>
    </el-table>
    <!--分页组件-->
    <pagination />
  </div>
</template>

<script>
import crudUserFund from '@/api/lifetools/userfund'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import Log from './log'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'
import { dateFormat, thousands } from '@/utils/index'

// crud交由presenter持有
const defaultForm = { id: null, fundCode: null, fundName: null, fundType: 1, purchaseChannel: 1, principalNetValue: null, count: null }
export default {
  name: 'UserFund',
  components: { Log, crudOperation, rrOperation, udOperation, pagination },
  cruds() {
    return CRUD({
      title: '我的基金',
      url: 'api/userfund',
      optShow: {
        add: true,
        edit: true,
        del: true,
        download: false,
        reset: true
      },
      query: {
        fundType: 1,
        sortDirection: 'asc',
        sortBy: 'id'
      },
      crudMethod: { ...crudUserFund }
    })
  },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  data() {
    return {
      userFunds: [],
      rules: {
        fundCode: [
          { required: true, message: '请输入基金编码', trigger: 'blur' }
        ],
        principalNetValue: [
          { required: true, message: '请输入持仓成本', trigger: 'blur' }
        ],
        count: [
          { required: true, message: '请输入持仓份额', trigger: 'blur' }
        ]
      },
      options: [{
        value: 1,
        label: '股票基金'
      }, {
        value: 2,
        label: '债券基金'
      }],
      channelOptions: [{
        value: 1,
        label: '蚂蚁财富'
      }, {
        value: 2,
        label: '天天基金'
      }, {
        value: 3,
        label: '京东金融'
      }, {
        value: 4,
        label: '招商银行'
      }],
      sortOptions: [{
        value: 'id',
        label: '默认时间排序'
      }, {
        value: 'profitRate',
        label: '持有收益率排序'
      }, {
        value: 'profitAmountOfYesterday',
        label: '昨日收益排序'
      }, {
        value: 'profitRateOfYesterday',
        label: '昨日收益率排序'
      }],
      sortDirection: [{
        value: 'asc',
        label: 'asc'
      }, {
        value: 'desc',
        label: 'desc'
      }],
      permission: {
        add: ['admin', 'userfund:add'],
        edit: ['admin', 'userfund:edit'],
        del: ['admin', 'userfund:del']
      }
    }
  },
  filters: {
    rounding(value) {
      return value.toFixed(2)
    },
    thousands(value) {
      return thousands(value)
    },
    formatDate(value) {
      return dateFormat(new Date(value), 'mm-dd')
    }
  },
  methods: {
    // 获取数据前设置好接口地址
    [CRUD.HOOK.beforeRefresh](curd) {
      const sortBy = curd.query.sortBy + ',' + curd.query.sortDirection
      if (curd.query.sortBy === 'id') {
        const sorts = []
        sorts.push(sortBy)
        curd.sort = sorts
      }
      return true
    },
    tableRowClassName({ row, rowIndex }) {
      if (rowIndex === 0) {
        return 'warning-row'
      }
      return ''
    },
    // 显示日志
    doLog() {
      this.$refs.log.dialog = true
      this.$refs.log.doInit()
    },
    // 选中图标
    selected(name) {
      this.form.icon = name
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
  ::v-deep .el-table .warning-row {
    background: #E6A23C;
  }
 ::v-deep .el-input-number .el-input__inner {
    text-align: left;
  }
 ::v-deep .vue-treeselect__control,::v-deep .vue-treeselect__placeholder,::v-deep .vue-treeselect__single-value {
    height: 30px;
    line-height: 30px;
  }
</style>
